<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="请根据类型名称查询"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="types" @row-dblclick="openDrawer" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="类型编号" width="120" >
      </el-table-column>
      <el-table-column prop="name" label="类型名称">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>

      <!--分页组件-->
			<el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :current-page="currentPage"
                     :total="totals"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑或者新增界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="类型编号" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>



<!--=========================================抽屉=========================================================-->
    <!--  抽屉表格的表头
       :append-to-body="true"-->
    <el-drawer
        title="数据明细抽屉"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <!--抽屉列表的表头-->
      <el-table :data="details">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="数据明细名字">
        </el-table-column>
<!--        <el-table-column prop="type.name" label="所属类型">-->
<!--        </el-table-column>-->
      </el-table>
    </el-drawer>
    <!--  抽屉表格的表脚-->
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	 import { getTypesListPage, removeTypes, batchRemoveTypes, editTypes, addTypes } from '../../api/api';

	export default {
		data() {
			return {
//====================================================以下是抽屉===========================================================
        // 抽屉所有属性
        table: false,
        dialog: false,
        loading: false,
        details: [], // 获取抽屉的数据
//======================================================以上是抽屉===========================================================
        // 字典类型查询
				filters: {
          keyword: ''
				},
        types: [], // 字典类型数据
        type:[], // 自定义type,否则会报错====================================type
				totals: 0, // 字典类型数据总条数
        pageSize: 5, // 字典类型每页展示条数
        currentPage:1, // 字典类型页码
				listLoading: false, // 忙等关闭
				sels: [],//列表选中列
        title:"", // 字典类型修改或者编辑的标题

        // 字典类型新增或者编辑弹框
				editFormVisible: false,// 字典类型新增或者编辑界面是否显示
				editLoading: false, // 字典类型新增或者编辑忙等关闭
				editFormRules: { // 字典类型表单中的字段校验
          sn: [
            { required: true, message: '请输入字典类型编号', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入字典类型名称', trigger: 'blur' }
          ],
        },
				// 字典类型编辑界面数据
				editForm: {
					id: null,
					sn: '',
          name:""
        },
      }
		},


		methods: {
//=========================================================以下是抽屉================================================
      // 打开抽屉
      openDrawer(row){
        this.table=true
        // 发送请求获取detail数据
        this.$http.get("/detail/loadType/" + row.id)
            .then(result=>{
              this.details=result.data
            })
            .catch(result=>{
              this.$message({
                message: '系统繁忙,请稍后重试!',
                type: 'error'
              });
            })
      },

// ========================================================以上是抽屉=======================================================
      // 字典类型查询函数
      search() {
        this.currentPage=1
        this.getTypes()
      },

			// 页码触发事件
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getTypes();
			},

			//获取字典类型数据
			getTypes() {
				let para = {
          pageSize: this.pageSize,
          currentPage:this.currentPage,
          keyword: this.filters.keyword
				};
				this.listLoading = true;
			  // 发送axios请求并且获取后端所有数据
        this.$http.post("/typed", para)
        .then(result=>{
          this.listLoading=false // 页表展示关闭忙等,因为在data总开启了忙等，发情请求就直接给data中的字段赋值
          this.types=result.data.rows
          this.totals=result.data.totals
        })
        .catch(result=>{
          this.$message({
            message: '系统繁忙,请稍后重试!',
            type: 'error'
          });
        })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
				  // 发送删除axios请求
          this.$http.delete("/typed/"+ row.id)
          .then(result=>{
            // 关闭忙等
            this.listLoading = false;
            // 前端给后端发送请求成功
            if(result.data.success){
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              // 返回第一页
              this.currentPage=1
              // 刷新列表
              this.getTypes()
            } else { // 发送失败
              this.$message({
                message: result.data.msg,
                type: 'error'
              });
            }
          }).catch(result=>{
            this.listLoading=false
            this.$message({
              message: '系统繁忙，请重试！',
              type: 'error'
            });
          })

				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title="字典类型编辑";
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			// 字典类型显示新增界面
			handleAdd: function () {
        this.title="字典类型新增";
        this.editFormVisible = true;
				this.editForm = {
          id: null,
          name: '',
          sn:"",

				};
			},

			// 字典类型编辑或者新增发送axios请求
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
						  // 发送请求
              this.$http.put("/typed", para)
              .then(result=>{
                this.editLoading = false; // 关闭忙等
                this.editFormVisible = false; // 关闭弹框
                if(result.data.success){
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  });
                  // 返回第一页
                  this.currentPage=1
                  // 刷新列表
                  this.getTypes()
                } else { // 发送失败
                  this.$message({
                    message: result.data.msg,
                    type: 'error'
                  });
                }

              })
              .catch(result=>{
                this.editLoading = false; // 关闭忙等
                this.editFormVisible = false; // 关闭弹框
                this.$message({
                  message: '系统繁忙，请稍后重试！',
                  type: 'error'
                });

              })
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
			    // 发送批量删除axios请求
          this.$http.patch("/typed", ids)
              .then(result=>{
                // 关闭忙等
                this.listLoading = false;
                // 前端给后端发送请求成功
                if(result.data.success){
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  });
                  // 返回第一页
                  this.currentPage=1
                  // 刷新列表
                  this.getTypes()
                } else { // 发送失败
                  this.$message({
                    message: result.data.msg,
                    type: 'error'
                  });
                }
              }).catch(result=>{
            this.listLoading=false
            this.$message({
              message: '系统繁忙，请稍后重试！',
              type: 'error'
            });
          })
        }).catch(() => {
        });
			},

    },


    // 钩子函数
		mounted() {
      // 字典类型数据
      this.getTypes();
    }
	}

</script>

<style scoped>

</style>